package com.zju.cst.cloudApp.mockSys;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

import com.zju.cst.cloudApp.entity.Message;
import com.zju.cst.cloudApp.util.ParamsKey;
import com.zju.cst.cloudApp.util.XmlUtil;

public class MockSendMsg {

	private static String HOST = "127.0.0.1";
	private static int PORT = 8888;
	
	public static void main(String[] args) {

		ChannelFactory factory = new NioClientSocketChannelFactory(
				Executors.newCachedThreadPool(),
				Executors.newCachedThreadPool());
		
		ClientBootstrap bootstrap = new ClientBootstrap(factory);
		
		MessageClientPipelineFactory pipelineFactory = new MessageClientPipelineFactory();
		pipelineFactory.setMessage(constructRegisteMsg());
		
		bootstrap.setPipelineFactory(pipelineFactory);		
		bootstrap.setOption("tcpNoDelay", false);
		bootstrap.setOption("keepAlive", false);
		
		ChannelFuture future = bootstrap.connect(new InetSocketAddress(HOST, PORT));

		future.getChannel().getCloseFuture().awaitUninterruptibly();
		bootstrap.releaseExternalResources();
	}
	

	public static String constructRegisteMsg() {
		Message message = new Message();
		String msgStr = "";
		message.setCode("10000001");
		message.setSession("8906157299");
		message.setType("00001");
		message.setUserId("yhqi");
		message.setParam(ParamsKey.HOST_NAME, "zxpanHost");
		message.setParam(ParamsKey.HOST_COMMENTS, "v1");
		message.setParam(ParamsKey.HOST_CPU, "2.2");
		message.setParam(ParamsKey.HOST_CPU_INIT, "2.3");
		message.setParam(ParamsKey.HOST_MEM_INIT, "1024");
		message.setParam(ParamsKey.HOST_DISK_INIT, "123.6");
		msgStr = XmlUtil.obj2Xml(message);
		return msgStr;
	}
}

